package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingList;
import com.jy.adscm.zltx.bean.JyDrawingRelation;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingListMapper;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingRelationMapper;
import com.jy.adscm.zltx.service.JyDrawingRelationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-02-21
 */
@Service
public class JyDrawingRelationServiceImpl extends ServiceImpl<JyDrawingRelationMapper, JyDrawingRelation> implements JyDrawingRelationService {
    @Autowired
    JyDrawingListMapper jyDrawingListMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertJyDrawingRelations(List<JyDrawingRelation> jyDrawingRelations) throws AdscmSystemException{
        //查询最近修改的的版本以他为模板来覆盖其他的图纸
          List<JyDrawingList> jyDrawingLists=new ArrayList<>();
        for (JyDrawingRelation jyDrawingRelation : jyDrawingRelations) {
            JyDrawingList jyDrawingList = jyDrawingListMapper.selectById(jyDrawingRelation.getRelationId());
            jyDrawingLists.add(jyDrawingList);
        }
        JyDrawingList  temp=null;
        for (JyDrawingList jyDrawingList : jyDrawingLists) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingList.getLastEditTime(),null)){
                if (AdscmBaseUtils.getInstance().isNotEmpty(temp,null)){
                  if (temp.getLastEditTime().getTime()<jyDrawingList.getLastEditTime().getTime()){
                      temp=jyDrawingList;
                  }
                }else {
                    temp=jyDrawingList;
                }
            }
        }
        //删除之前这个图纸所有的关系
        if(AdscmBaseUtils.getInstance().isNotEmpty(temp,null) ){

            Wrapper<JyDrawingRelation> wrapper= new EntityWrapper<>();
            wrapper.eq("relation_id",temp.getId());

            //查询出所有的关系
            List<JyDrawingRelation> oldjyDrawingRelations = super.selectList(wrapper);
            //删除关系
            if (oldjyDrawingRelations.size()>0){
                JyDrawingRelation relation = oldjyDrawingRelations.get(0);
                Wrapper<JyDrawingRelation> wrapperJyDrawingRelation= new EntityWrapper<>();
                wrapperJyDrawingRelation.eq("drawing_id",relation.getDrawingId());
                super.delete(wrapperJyDrawingRelation);
            }
        }else {
            throw new AdscmSystemException("图纸的最近修改时间为空不能去关联");
        }

        //查询数据库最大的drawing_id
         Wrapper<JyDrawingRelation> jyDrawingRelationWrapper =new EntityWrapper<>();
         jyDrawingRelationWrapper.orderBy("drawing_id",false);

         List<JyDrawingRelation> jyDrawingRelations1 = super.selectList(jyDrawingRelationWrapper);
         int drawing_id=1;
         if (jyDrawingRelations1.size()>0){
             drawing_id=  jyDrawingRelations1.get(0).getDrawingId()+1;
         }



         for (JyDrawingRelation drawingRelation : jyDrawingRelations) {
             drawingRelation.setDrawingId(drawing_id);
            super.insert(drawingRelation);
            //根据现在的关系更新相关联的图纸
            JyDrawingList jyDrawingListRelation = jyDrawingListMapper.selectById(Long.valueOf(drawingRelation.getRelationId()));
            jyDrawingListRelation.setDrawingPath(temp.getDrawingPath());
            jyDrawingListMapper.updateById(jyDrawingListRelation);
        }

        return true;
    }
}
